package cn.detu.ln.controller;


import cn.detu.ln.entity.Admin;
import cn.detu.ln.service.AdminService;
import cn.detu.ln.utils.PageUtil;
import cn.detu.ln.utils.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 控制层：请求的接收和响应
 * Restful风格：  GET   POST   PUT   DELETE
 */
@RestController
@RequestMapping("admin")  // 封装第一层路径
public class AdminController {
    // 由于controller层需要调用service层方法，因此需要自动装配一个UserService对象
    @Autowired
    private AdminService adminService;
    // GET请求获取用户列表数据
    // localhost:1314/user/selectAll
    @GetMapping("{url}")
    public RestUtil userGet(@PathVariable String url, Admin admin, PageUtil pageUtil, String search){
        switch (url){
            case "selectAll": return adminService.selectAll();
            case "selectOne": return adminService.select(admin.getAdminId());
            case "adminDelete":return adminService.adminDelete(admin.getAdminId());
            case "queryAdminByPage":return adminService.queryAdminByPage(pageUtil.getCurrent(),pageUtil.getRows(),
                    search);
        }

        return null;
    }
    @PostMapping("{url}")
    public RestUtil userPost(@PathVariable String url, Admin admin,String newPwd){
        switch (url){
            case "adminUpdate":return adminService.adminUpdate(admin);
            case "adminInsert":return adminService.adminInsert(admin);
            case "login":
                return adminService.login(admin);
            case "regist":
                return adminService.regist(admin);
            case "updatePwd":return adminService.adminUpdatePwd(admin,newPwd);
        }
        return null;
    }
    // GET请求根据userId获取单条数据
    // localhost:1314/user/queryUserById/1
    @GetMapping("queryAdminById/{adminId}")
    public Admin queryAdminById(@PathVariable("adminId") Integer adminId){
        return adminService.queryAdminById(adminId);
    }

}
